package com.ruoyi.standingbook.extermination.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.standingbook.extermination.domain.Extermination;
import com.ruoyi.standingbook.recycle.domain.RecycleImportDetail;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.extermination.domain.bo.ExterminationImportDetailBo;
import com.ruoyi.standingbook.extermination.domain.vo.ExterminationImportDetailVo;
import com.ruoyi.standingbook.extermination.domain.ExterminationImportDetail;
import com.ruoyi.standingbook.extermination.mapper.ExterminationImportDetailMapper;
import com.ruoyi.standingbook.extermination.service.IExterminationImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 四害消杀导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-30
 */
@RequiredArgsConstructor
@Service
public class ExterminationImportDetailServiceImpl implements IExterminationImportDetailService {

    private final ExterminationImportDetailMapper baseMapper;

    /**
     * 查询四害消杀导入明细
     */
    @Override
    public ExterminationImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询四害消杀导入明细列表
     */
    @Override
    public TableDataInfo<ExterminationImportDetailVo> queryPageList(ExterminationImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExterminationImportDetail> lqw = buildQueryWrapper(bo);
        Page<ExterminationImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询四害消杀导入明细列表
     */
    @Override
    public List<ExterminationImportDetailVo> queryList(ExterminationImportDetailBo bo) {
        LambdaQueryWrapper<ExterminationImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExterminationImportDetail> buildQueryWrapper(ExterminationImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExterminationImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBatchNo()), ExterminationImportDetail::getBatchNo, bo.getBatchNo());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ExterminationImportDetail::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getExterminationCategory()), ExterminationImportDetail::getExterminationCategory, bo.getExterminationCategory());
        lqw.eq(bo.getProcessStatus() != null, ExterminationImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
            ExterminationImportDetail::getDate ,params.get("beginDate"), params.get("endDate"));
        lqw.orderByDesc(ExterminationImportDetail::getCreateTime);
        lqw.orderByDesc(ExterminationImportDetail::getDate);
        return lqw;
    }

    /**
     * 新增四害消杀导入明细
     */
    @Override
    public Boolean insertByBo(ExterminationImportDetailBo bo) {
        ExterminationImportDetail add = BeanUtil.toBean(bo, ExterminationImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改四害消杀导入明细
     */
    @Override
    public Boolean updateByBo(ExterminationImportDetailBo bo) {
        ExterminationImportDetail update = BeanUtil.toBean(bo, ExterminationImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExterminationImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除四害消杀导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量插入
     */
    @Override
    public Boolean batchInsertVo(List<ExterminationImportDetailVo> voList) {
        List<ExterminationImportDetail> list= BeanCopyUtils.copyList(voList , ExterminationImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
